﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LiveCharts;
using LiveCharts.Definitions.Series;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace Wpf_Examples.ViewModels
{
    public class MultiYChartViewModel:ObservableObject
    {
        public ObservableCollection<ISeriesView> Series { get; set; }
        public ObservableCollection<Axis> XAxes { get; set; }
        public ObservableCollection<Axis> YAxes { get; set; }

        private Axis selectedYAxis;
        public Axis SelectedYAxis {
            get { return selectedYAxis; }
            set
            {
                SetProperty(ref selectedYAxis, value);
            }
        }

        public RelayCommand AddYAxisCommand { get; set; }
        public RelayCommand RemoveYAxisCommand { get; set; }

        public MultiYChartViewModel() {

            AddYAxisCommand = new RelayCommand(AddYAxisDefault);
            RemoveYAxisCommand = new RelayCommand(RemoveYAxis);
            SelectedYAxis =new Axis();
            Series = new ObservableCollection<ISeriesView>();
            XAxes = new ObservableCollection<Axis>
            {
                new Axis
                {
                    Title = "X Axis",
                    MinValue = 0,
                    MaxValue = 10
                }
            };
            YAxes = new ObservableCollection<Axis>();

            // 初始化一些Y轴和数据系列
            AddYAxis("Y Axis 1");
            AddYAxis("Y Axis 2");

            AddSeries("Series 1", "Y Axis 1", new[] { 1.0, 3.0, 5, 7, 9 }, Brushes.Red);
            AddSeries("Series 2", "Y Axis 1", new[] { 2, 4.0, 6, 8, 10 }, Brushes.Blue);
            AddSeries("Series 3", "Y Axis 2", new[] { 1.5, 3.5, 5.5, 7.5, 9.5 }, Brushes.Green);

        }

        private void AddYAxisDefault()
        {
            var newAxis = new Axis
            {
                Title = "系列",
                MinValue = 0,
                MaxValue = 10
            };
            YAxes.Add(newAxis);
        }
        private void AddYAxis(string title=null)
        {
            if (title == null) title = "系列";
            var newAxis = new Axis
            {
                Title = title,
                MinValue = 0,
                MaxValue = 10
            };
            YAxes.Add(newAxis);
        }

        private void RemoveYAxis()
        {
            if (SelectedYAxis != null && YAxes.Contains(SelectedYAxis))
            {
                YAxes.Remove(SelectedYAxis);

                // 移除与该Y轴关联的所有数据系列
                var seriesToRemove = Series.Where(s => s == SelectedYAxis).ToList();
                foreach (var series in seriesToRemove)
                {
                    Series.Remove(series);
                }
            }
        }

        public void AddSeries(string name, string yAxisTitle, double[] data, Brush color)
        {
            var yAxis = YAxes.FirstOrDefault(y => y.Title == yAxisTitle);
            if (yAxis != null)
            {
                var lineSeries = new LineSeries
                {
                    Title = name,
                    Values = new ChartValues<double>(data),
                    LineSmoothness = 0.2, // 平滑曲线
                    PointGeometry = null, // 不显示点
                    Stroke = color
                };

                Series.Add(lineSeries);
            }
        }
    }
}
